#include "MATSSVEP_CSP.h"
#include "SSVEP_Dialog.h"



MatSSVEP_CSP::MatSSVEP_CSP(void):MatAlgorithm()
{

	wxString Temp;
	wxFileDialog Dialog(NULL);
	
//	OpenMatlab();
	EvalString(_("Dati = [];Out = zeros(4,1);"));

	Filter.LoadFromFile(_T("./filters/SSVEP_Band5-40_5.f"));
	EnableClassification = EnableFeedback = true;

	Error = false;

	wxString Name;
	Dialog.SetWildcard(_T("XDA Training files   (*.xda)|*.xda"));
	if(wxID_OK == Dialog.ShowModal())
	{
		Name = Dialog.GetPath();
		EvalString(_("T = importdata('")+Name+_("');"));
		EvalString(_("RES=T.RES;"));

		EvalString(_("Periods=single(RES.StimPeriods);"));
		mPeriods.rows	=	1;
		mPeriods.cols	=	4;
		mPeriods.esize	=	sizeof(float);
		mPeriods.pdata	=	fPeriods;
		int T[4];
		GetFromMatlab("Periods",&mPeriods);
		for(int i=0;i<4;i++)T[i] = fPeriods[i];
		SetPeriods(T);

		EvalString(_("Spatial=single(RES.SpatialFilt);"));
		mSpatialFilter.rows	=	1;
		mSpatialFilter.cols	=	8;
		mSpatialFilter.esize	=	sizeof(float);
		mSpatialFilter.pdata	=	SpatialFilter;

		GetFromMatlab("Spatial",&mSpatialFilter);

		EvalString(_("WinLen=single(RES.WinLen);"));
		mWinLen.rows	=	1;
		mWinLen.cols	=	1;
		mWinLen.esize	=	sizeof(float);
		mWinLen.pdata	=	&WinLen;		
		GetFromMatlab("WinLen",&mWinLen);

		Open(WinLen,WinLen,8,4,1);
		Error = false;
	}
	else
	{
		Error = true;
		wxMessageBox(_("Error, you have not selected a .XDA file."),_("Error"));
	}
	SingleSample = false;
}

MatSSVEP_CSP::~MatSSVEP_CSP(void)
{
}

void MatSSVEP_CSP::SetPeriods(int *pPeriods)
{
	//Inserire la parte di matlab
	memcpy(&Periods[0],pPeriods,sizeof(Periods));	
}
void MatSSVEP_CSP::SendPeriods(void)
{
	//Inserire la parte di matlab

	BCIMessage Temp;
	Temp = ComposeMessage(SET_PARAMS,0,Periods,sizeof(Periods));
	WriteOut((char*)&Temp,sizeof(Temp));		
	
}
void MatSSVEP_CSP::PutSample(SampleStruct *pData)
{
	float Acc = 0;
	SampleStruct LocalData = *pData;
	for(int i=0;i<8;i++)
	{
		Acc += LocalData.Data.Signal[i]* SpatialFilter[i];	
	}
	
	LocalData.Data.Signal[0] =Acc;
	Filter.FilterSample(&LocalData.Data);
	//Mettere qua se serve filtro spaziale
	Algorithm::PutSample(&LocalData);
	pData->Algo = LocalData.Algo;
}
void MatSSVEP_CSP::RunSecWin(void)
{
	c4mMatrix	Out;
	float tFeed[4];
	Out.cols = 4;
	Out.rows = 1;
	Out.pdata = &tFeed;
	Out.esize = sizeof(float32);
	
	c4mMatrix	Class;
	float32 dClass;
	Class.cols = Class.rows = 1;
	Class.pdata = &dClass;
	Class.esize = sizeof(float32);
	

	//Inserisco i dati
	PutIntoMatlab("FeedWin",c4mSecWindow);
	ShiftSecondaryWin(64);

	//Elaboro i dati
	EvalString(_("Feature = single(MeanFeed256(FeedWin(:,1)',Periods));"));
	EvalString(_("Feedback = single(100*MeanFeed256_BaseRem(FeedWin(:,1)',Periods));"));
	GetFromMatlab("Feedback",&Out);
	SetFeedback(tFeed);
	if(EnableClassification)
	{
		EvalString(_("[Class, Tsd] = ApplyXda(Feature,RES);"));
		GetFromMatlab("Class",&Class);
		if(dClass != 0)
		{
			SetClassification(dClass);
		}
	}
	
	
	//pMatlab->EvalString("len = length(Dati)-1000; if(len<1)len = 1;end;plot(Dati(:,len:end)');");
}

void MatSSVEP_CSP::RunMainWin(void)
{

	ShiftMainWin(256);
/*
	pMatlab->EvalString("[Class, Tsd] = single(ApplyXda(Feedback,RES);)");
	GetFromMatlab("Out",&Class);
	if(dClass != 0)
	{
		SetClassification(dClass);
	}*/
}



